package com.ruoyi.web.controller.warning;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.warning.WarningAddressBook;
import com.ruoyi.web.domain.warning.WarningNotice;
import com.ruoyi.web.pojo.request.warning.WarningAddressBookRequest;
import com.ruoyi.web.pojo.request.warning.WarningNoticeRequest;
import com.ruoyi.web.service.warning.WarningAddressBookService;
import com.ruoyi.web.service.warning.WarningNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(value = "预警通知管理",tags = "预警通知管理")
@RestController
@RequestMapping("/warning-notice")
public class WarningNoticeController extends BaseController {

    @Autowired
    private WarningAddressBookService warningAddressBookService;

    @Autowired
    private WarningNoticeService warningNoticeService;

    @ApiOperation("分页获取通讯录列表")
    @GetMapping("/addressBookPage")
    public TableDataInfo<WarningAddressBook> addressBookPage(WarningAddressBookRequest request){
        startPage();
        List<WarningAddressBook> warningAddressBooks = warningAddressBookService.queryPage(request);
        return getDataTable(warningAddressBooks);
    }

    @PostMapping("/addOrUpdateAddressBook")
    @ApiOperation("新增或修改通讯录")
    public AjaxResult addOrUpdateAddressBook(@RequestBody WarningAddressBook config) {
        warningAddressBookService.updateOrSaveBook(config);
        return success();
    }

    @ApiOperation("批量删除通讯录")
    @DeleteMapping("/removeAddressBook")
    public AjaxResult removeAddressBook(@RequestParam String ids)
    {
        warningAddressBookService.delete(ids);
        return success();
    }

    @ApiOperation("导入通讯录")
    @PostMapping("/importAddressBook")
    public AjaxResult importAddressBook(MultipartFile file) throws Exception
    {
        ExcelUtil<WarningAddressBook> util = new ExcelUtil<>(WarningAddressBook.class);
        List<WarningAddressBook> books = util.importExcel(file.getInputStream());
        warningAddressBookService.addImport(books);
        return AjaxResult.success();
    }


    @ApiOperation("分页获取预警通知列表")
    @GetMapping("/warningNoticePage")
    public TableDataInfo<WarningNotice> warningNoticePage(WarningNoticeRequest request){
        startPage();
        List<WarningNotice> warningNotices = warningNoticeService.queryPage(request);
        return getDataTable(warningNotices);
    }

    @PostMapping("/addOrUpdateWarningNotice")
    @ApiOperation("新增或修改预警通知")
    public AjaxResult addOrUpdateWarningNotice(@RequestBody WarningNotice notice) {
        warningNoticeService.addOrUpdateNotice(notice);
        return success();
    }

    @ApiOperation("批量删除预警通知")
    @DeleteMapping("/removeWarningNotice")
    public AjaxResult removeWarningNotice(@RequestParam String ids)
    {
        warningNoticeService.delete(ids);
        return success();
    }


}
